const mongoUtil = require("./mongo_util")
const collectionName = "order"

function getOrderList(userid, callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    // console.log("mmp=", mmp);
    // console.log(userid.page.length);
    if (userid.page) {
        mmp.then(collection => {
            collection.find({
                // 'userId': userid.userid
                // 'orderlist.orderNo':userid
            }).limit(Number(userid.pagesize)).skip((Number(userid.page)- 1) * Number(userid.pagesize)).sort({
                'orderlist.orderNo': -1
            }).toArray(function (err, result) {
                console.log("resultjjjj", result);
                if (result.length) {
                    callback(null, result)
                    // new Error()
                } else {
                    callback(err ? err : {
                        reason: "还没有没有订单"
                    }, result)
                }
            })
        })
    } else {
        mmp.then(collection => {
            collection.find({
                    'userId': userid
                    // 'orderlist.orderNo':userid
                }).toArray(function (err, result) {
                    // console.log("resultjjjj", result);
                    if (result.length) {
                        callback(null, result)
                        // new Error()
                    } else {
                        callback(err ? err : {
                            reason: "该用户还没有订单"
                        }, result)
                    }
                })
        })
    }

}

function getuserOrderList(orderNo, callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    console.log("mmp=", mmp);
    console.log("orderNo", orderNo);
    mmp.then(collection => {
        collection.find({
            // 'orderNo': orderNo
            'orderlist.orderNo': orderNo
        }).toArray(function (err, result) {
            // console.log("resultjjjj", result);
            if (result.length) {
                callback(null, result)
                // new Error()
            } else {
                callback(err ? err : {
                    reason: "该用户还没有订单"
                }, result)
            }
        })
    })
}
//后台修改订单
function putuserOrderList(putlist, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({}).toArray(function (err, result) {
            // console.log("=====",result);
            result.forEach(item => {
                item.orderlist.filter(ite => {
                    // console.log("putlist.orderNo", putlist.orderNo.includes(ite.orderNo));
                    return putlist.orderNo.includes(ite.orderNo)
                }).forEach(pre => {

                    for (var key in putlist.orderNo) {
                        // console.log(putlist.orderNo[key]);
                        if (pre.orderNo == putlist.orderNo[key]) {
                            pre.orderStatusString = putlist.status
                            console.log('ite.orderStatusString', item.orderlist);
                            var whereStr = {
                                'orderlist.orderNo': putlist.orderNo[key],
                            }
                            var updateStr = {
                                $set: {

                                    'orderlist': item.orderlist
                                }
                            };
                            mongoUtil.getCollection(collectionName).then(collection => {
                                collection.updateOne(whereStr, updateStr, function (err, res) {
                                    callback(err, res)
                                });
                            })
                        }
                    }

                })
            })
        })
    })
}
module.exports = {
    getOrderList,
    getuserOrderList,
    putuserOrderList
}